package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.business.dto.Param;
import com.lvy.hczn.front.business.dto.SortTaskDetailDto;
import com.lvy.hczn.front.business.mapper.ProductionProcessingMapper;
import com.lvy.hczn.front.business.mapper.SortTaskDetailMapper;
import com.lvy.hczn.front.business.mapper.SortTaskMapper;
import com.lvy.hczn.front.business.service.ProductionProcessingService;
import com.lvy.hczn.front.common.core.text.Convert;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 生产加工任务绑定Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-23
 */
@Service
public class ProductionProcessingServiceImpl extends ServiceImpl<ProductionProcessingMapper, ProductionProcessing> implements ProductionProcessingService {

    @Autowired
    private ProductionProcessingMapper productionProcessingMapper;

    @Autowired
    private SortTaskMapper sortTaskMapper;

    @Autowired
    private SortTaskDetailMapper sortTaskDetailMapper;

    /**
     * 查询生产加工任务绑定
     *
     * @param id 生产加工任务绑定主键
     * @return 生产加工任务绑定
     */
    @Override
    public ProductionProcessing selectProductionProcessingById(String id) {
        return productionProcessingMapper.selectProductionProcessingById(id);
    }

    /**
     * 查询生产加工任务绑定列表
     *
     * @param productionProcessing 生产加工任务绑定
     * @return 生产加工任务绑定
     */
    @Override
    public List<ProductionProcessing> selectProductionProcessingList(ProductionProcessing productionProcessing) {
        return productionProcessingMapper.selectProductionProcessingList(productionProcessing);
    }

    /**
     * 新增生产加工任务绑定
     *
     * @param productionProcessing 生产加工任务绑定
     * @return 结果
     */
    @Override
    public int insertProductionProcessing(ProductionProcessing productionProcessing) {
        return productionProcessingMapper.insert(productionProcessing);
    }

    /**
     * 修改生产加工任务绑定
     *
     * @param productionProcessing 生产加工任务绑定
     * @return 结果
     */
    @Override
    public int updateProductionProcessing(ProductionProcessing productionProcessing) {
        return productionProcessingMapper.updateById(productionProcessing);
    }

    /**
     * 批量删除生产加工任务绑定
     *
     * @param ids 需要删除的生产加工任务绑定主键
     * @return 结果
     */
    @Override
    public int deleteProductionProcessingByIds(String ids) {
        return productionProcessingMapper.deleteProductionProcessingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除生产加工任务绑定信息
     *
     * @param id 生产加工任务绑定主键
     * @return 结果
     */
    @Override
    public int deleteProductionProcessingById(String id) {
        return productionProcessingMapper.deleteProductionProcessingById(id);
    }

    /**
     * 生成生产加工任务绑定信息，
     * 离开缓存位后，根据此表统计货箱详情，一样的商品信息，sum一下，ProductionProcessing数据改为无效
     *
     * @param sortTask       分拣主表
     * @param sortTaskDetail 分拣详情
     */
    @Override
    public boolean taskProductionProcessing(SortTask sortTask, SortTaskDetailDto sortTaskDetail, GoodsBoxDetail goodsBoxDetail, BigDecimal weight, int quantity) {
        ProductionProcessing productionProcessing = new ProductionProcessing();
        productionProcessing.setSortTaskDetailId(sortTaskDetail.getId());
        productionProcessing.setServiceCode(goodsBoxDetail.getEntryCode());
        productionProcessing.setClientId(sortTaskDetail.getClientId());
        productionProcessing.setBoxCode(goodsBoxDetail.getBoxCode());
        productionProcessing.setSpuId(sortTaskDetail.getSpuId());
        productionProcessing.setSkuCode(sortTaskDetail.getSkuCode());
        productionProcessing.setSpecCode(sortTaskDetail.getSpecCode());
        productionProcessing.setWeightOfPerBox(sortTaskDetail.getWeight());
        productionProcessing.setTotalWeight(weight);
        productionProcessing.setQuantity(quantity);
        productionProcessing.setMaterialProcessId(sortTaskDetail.getMaterialProcessId());
        productionProcessing.setSpuGradeId(goodsBoxDetail.getSpuGradeId());
        return super.save(productionProcessing);
    }

    /**
     * 根据时间段物理删除
     *
     * @param productionProcessing 分拣主表
     */
    @Override
    public int physicalDataDelete(ProductionProcessing productionProcessing) {
        if (productionProcessing.getCreateTime() != null && productionProcessing.getUpdateTime() != null) {
            return productionProcessingMapper.physicalDataDelete(productionProcessing);
        } else {
            throw new UtilException("参数为空");
        }
    }

    /**
     * 生产加工新原料箱列表查询
     *
     * @param param 参数
     */
    @Override
    public List<ProductionProcessing> selectProcessingEntryListByBoxCode(Param param) {
        GoodsBox goodsBox = param.getGoodsBox();
        if (goodsBox == null || StrUtil.isEmpty(goodsBox.getBoxCode())) {
            throw new UtilException("箱码为空");
        }
        ProductionProcessing productionProcessing = new ProductionProcessing();
        productionProcessing.setBoxCode(goodsBox.getBoxCode());
        productionProcessing.setDone(false);
        return this.selectProductionProcessingList(productionProcessing);
    }

    /**
     * 货箱离开入库后更新
     *
     * @param upOrDownPolicy
     * @throws Exception
     */
    @Override
    public void processingEntryComplete(UpOrDownPolicy upOrDownPolicy) throws Exception {
        if (ObjUtil.isNull(upOrDownPolicy) || StrUtil.isEmpty(upOrDownPolicy.getBoxCode())) {
            throw new UtilException("货箱编码为空");
        }
        LambdaUpdateWrapper<ProductionProcessing> lambdaupdateWrapper = new LambdaUpdateWrapper<>();
        lambdaupdateWrapper.eq(ProductionProcessing::getBoxCode, upOrDownPolicy.getBoxCode());
        lambdaupdateWrapper.eq(ProductionProcessing::getClientId, upOrDownPolicy.getClientId());
        lambdaupdateWrapper.eq(ProductionProcessing::getDone, false);
        lambdaupdateWrapper.set(ProductionProcessing::getDone, true);
        super.update(lambdaupdateWrapper);
    }
}
